<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2022/2/14
 * Time: 15:57
 */

namespace app\admin\controller;


use think\Db;

class Pan extends Base
{
    /**
     * 盘店记录
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $pan_username = input('pan_username');
            $shop_name = input('shop_name');

            $where = [];
            if(isset($pan_username) && $pan_username != ''){
                $where['u2.user_username'] = ['like', '%' . $pan_username . '%'];
            }
            if(isset($shop_name) && $shop_name != ''){
                $where['u1.user_shop_name'] = ['like', '%' . $shop_name . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('goods_pan')
                ->join('up_u_user u1', 'gp_user_id = u1.user_id', 'LEFT')
                ->join('up_u_user u2', 'gp_teacher_id = u2.user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('goods_pan')
                ->join('up_u_user u1', 'gp_user_id = u1.user_id', 'LEFT')
                ->join('up_u_user u2', 'gp_teacher_id = u2.user_id', 'LEFT')
                ->where($where)
                ->field('gp_id, gp_date, u1.user_shop_name, u2.user_username')
                ->order('gp_date desc')
                ->page($Nowpage, $limits)
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 盘店记录
     * @return mixed|\think\response\Json
     */
    public function panInfo(){
        if(request()->isAjax ()){
            $old_user_shop = input('old_user_shop');
            $old_user = input('old_user');
            $bar_code = input('bar_code');
            $fw_code = input('fw_code');

            $where['sp_gp_id'] = session('gp_id');
            if(isset($old_user_shop) && $old_user_shop != ''){
                $where['u1.user_shop_name'] = ['like', '%' . $old_user_shop . '%'];
            }
            if(isset($old_user) && $old_user != ''){
                $where['u1.user_username'] = ['like', '%' . $old_user . '%'];
            }
            if(isset($bar_code) && $bar_code != ''){
                $where['sp_bar_code'] = ['like', '%' . $bar_code . '%'];
            }
            if(isset($fw_code) && $fw_code != ''){
                $where['sp_fw_bar_code'] = ['like', '%' . $fw_code . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('goods_stock_pan')
                ->join('up_bo_goods', 'bg_id = sp_goods_id', 'LEFT')
                ->join('up_u_user u1', 'sp_old_user_id = u1.user_id', 'LEFT')
                ->join('up_u_user u2', 'sp_now_user_id = u2.user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('goods_stock_pan')
                ->join('up_bo_goods', 'bg_id = sp_goods_id', 'LEFT')
                ->join('up_u_user u1', 'sp_old_user_id = u1.user_id', 'LEFT')
                ->join('up_u_user u2', 'sp_now_user_id = u2.user_id', 'LEFT')
                ->where($where)
                ->field('sp_id, sp_bar_code, u1.user_username old_name, u1.user_shop_name old_shop, u2.user_username new_name, 
                u2.user_shop_name new_shop, sp_type, bg_name, sp_fw_bar_code')
                ->order('sp_time desc')
                ->page($Nowpage, $limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $gp_id = input('gp_id');
        if($gp_id){
            session('gp_id', $gp_id);
        }
        return $this->fetch();
    }
}